package com.slw.caiji.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slw.caiji.Vo.params.PageParams;
import com.slw.caiji.dto.SysApp;
import com.slw.caiji.dto.SysApp;
import com.slw.caiji.mapper.SysAppMapper;
import com.slw.caiji.service.SysAppService;
import com.slw.caiji.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class SysAppServiceImpl implements SysAppService {



    @Autowired
    private SysAppMapper sysAppMapper;

    @Override
    public Result getSysAppList(PageParams pageParams) {
        final Page<SysApp> objectPage = new Page<>(pageParams.getCurrent(), pageParams.getPageSize());
        final IPage<SysApp> sysUserIPage = sysAppMapper.listSysApp(objectPage);
        return Result.success(sysUserIPage);
    }

    @Override
    public Result addApp(SysApp sysApp) {
        final SysApp sysApp1 = new SysApp();
        sysApp1.setAppName(sysApp.getAppName());
        sysApp1.setAppTypeId(sysApp.getAppTypeId());
        sysApp1.setCreatetime(new Date());
        sysApp1.setIcon(sysApp.getIcon());
        sysApp1.setAppCode(sysApp.getAppCode());
        sysApp1.setOrderNum(sysApp.getOrderNum());
        sysApp1.setAppDesc(sysApp.getAppDesc());
        return Result.success(sysAppMapper.insert(sysApp1));
    }

    @Override
    public Result delApp(String ids) {
        final String[] split = ids.split(",");
        for (String s : split) {
            sysAppMapper.deleteById(s);
        }
        return Result.success(1);
    }

    @Override
    public Result detailApp(String id) {
        SysApp sysApp = sysAppMapper.selectById(id);
        return Result.success(sysApp);
    }

    @Override
    public Result updateApp(SysApp sysApp) {
        SysApp sysApp2 = new SysApp();
        sysApp2.setAppName(sysApp.getAppName());
        sysApp2.setAppTypeId(sysApp.getAppTypeId());
        sysApp2.setCreatetime(new Date());
        sysApp2.setIcon(sysApp.getIcon());
        sysApp2.setAppCode(sysApp.getAppCode());
        sysApp2.setOrderNum(sysApp.getOrderNum());
        sysApp2.setAppDesc(sysApp.getAppDesc());
        sysApp2.setId(sysApp.getId());
        return Result.success(sysAppMapper.updateById(sysApp2));
    }
}
